﻿using Microsoft.EntityFrameworkCore;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

using ZFB.Models;
using ZFB.Models.EntityConfig;
using ZFB.Repository.Extensions;

namespace ZFB.Repository.HouseAppointments
{
    public class HouseAppointmentsRepository : BaseRepository<T_HouseAppointments>, IHouseAppointmentsRepository
    {
        private readonly MyDbContext _dbContext;
        public HouseAppointmentsRepository(MyDbContext db) : base(db)
        {
            this._dbContext = db;
        }
        public override async Task<List<T_HouseAppointments>> GetListAsync(Expression<Func<T_HouseAppointments, bool>> whereExpression)
        {
            return await _dbContext.T_HouseAppointments
                .AsNoTracking()
                .Where(e=>!e.IsDeleted)
                .Where(whereExpression)
                .Include(e=>e.T_Houses)
                .Include(e=>e.T_Houses.T_Communities)
                .Include(e=>e.T_AdminUsers)
                .Include(e=>e.T_Houses.T_Communities.T_Regions)
                .ToListAsync();
        }
        public async override Task<Page<T_HouseAppointments>> GetPagesAsync(int page, int limit, Expression<Func<T_HouseAppointments, bool>> whereExpression)
        {
            Page<T_HouseAppointments> pages = new();
            var count = await _dbContext.T_HouseAppointments
                .AsNoTracking()
                .Where(e => !e.IsDeleted)
                .Where (whereExpression)
                .LongCountAsync();
            pages.Items = await _dbContext.T_HouseAppointments
                .AsNoTracking()
                .Where(e => !e.IsDeleted)
                .Where(whereExpression)
                .Skip((page - 1) * limit)
                .Take(limit)
                .ToListAsync();
            pages.TotalItems = count;
            pages.TotalPages = (int)Math.Ceiling(count * 10f / limit);
            pages.ItemsPerPage = limit;
            pages.CurrentPage= page;

            return pages;
        }
    }
}
